package org.book.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.book.bean.Borrow;
import org.book.bean.User;
import org.book.bean.User;
import org.book.common.JsonResult;
import org.book.configuration.aop.NoCheckLogin;
import org.book.configuration.aop.PermissionCheck;
import org.book.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.Wrapper;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dancun
 * @since 2021-03-05
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    IUserService userService;

//    @RequestMapping(value = "login")
//    @NoCheckLogin
    public JsonResult login (User user, HttpServletRequest request){
        log.info("尝试登录");
        QueryWrapper<User> wrapper = Wrappers.query();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("password",user.getPassword());
        User loginuser = userService.getOne(wrapper);
        if(loginuser==null||loginuser.getUsername()==null||loginuser.getUsername().equals("")){
            return  JsonResult.renderFail("账号或密码错误");
        }
        request.getSession().setAttribute("user",loginuser.getUsername());
        loginuser.setPassword("");
        return  JsonResult.renderSuccess("登陆成功",loginuser);
    }


    @PostMapping("/list")
    @PermissionCheck
    public JsonResult list(String key,Integer pageNum, Integer pageSize){

        if(pageSize!=null&&pageSize!=0){
            if (pageNum==null){
                pageSize=0;
            }
            PageHelper.startPage(pageNum,pageSize);
        }
        PageInfo page = null;
        try{
           LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
           wrapper.select(User::getUsername,User::getName,User::getTel,User::getTimer);
           if(key!=null&&!key.equals("key")){
               wrapper.like(User::getUsername,key).or().like(User::getName,key);
           }
            page = new PageInfo<User>(userService.list(wrapper));
        }catch (Exception e){
            return  JsonResult.renderError(e.toString());
        }

        return JsonResult.renderSuccess(page);

    }

    @PostMapping("/listname")
    @PermissionCheck
    public JsonResult listname(Integer pageNum, Integer pageSize){

        if(pageSize!=null&&pageSize!=0){
            if (pageNum==null){
                pageSize=0;
            }
            PageHelper.startPage(pageNum,pageSize);
        }
        PageInfo page = null;
        try{
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper();
            wrapper.select(User::getUsername,User::getName);
            page = new PageInfo<User>(userService.list(wrapper));
        }catch (Exception e){
            return  JsonResult.renderError(e.toString());
        }

        return JsonResult.renderSuccess(page);

    }

    @PostMapping("/findById")
    public JsonResult findById(String isbn){
        User user = null;
        try{
            user = userService.getById(isbn);
        }catch (Exception e){
            log.info(e.getStackTrace().toString());
            return JsonResult.renderError(e.getStackTrace().toString());
        }
        return JsonResult.renderSuccess(user);
    }

    @PostMapping("/search")
    public JsonResult search(String key,Integer pageNum,Integer pageSize ) {


        if (pageSize != null && pageSize != 0) {
            if (pageNum == null) {
                pageSize = 0;
            }
            PageHelper.startPage(pageNum, pageSize);
        }
        PageInfo page = null;
        try {
            QueryWrapper wrapper = Wrappers.query();
            wrapper.like("username" ,key);
            page = new PageInfo<User>(userService.list(wrapper));
        } catch (Exception e) {
            return JsonResult.renderError(e.toString());
        }

        return JsonResult.renderSuccess(page);
    }
    @PostMapping("/insert")
    @NoCheckLogin
    public JsonResult insert(User user){
        try{
            userService.save(user);
            return JsonResult.renderSuccess("注册成功");
        }catch (Exception e){
            return JsonResult.renderFail("注册失败");
        }

    }

    @PostMapping("/update")
    public JsonResult update(User user){
        try{
            userService.updateById(user);
            return JsonResult.renderSuccess("修改用户信息成功");
        }catch (Exception e){
            return JsonResult.renderFail("修改用户信息失败");
        }

    }

    @PostMapping("/delete")
    public JsonResult delete(User user){
        try{
            userService.removeById(user.getUsername());
            return JsonResult.renderSuccess("删除成功");
        }catch (Exception e){
            return JsonResult.renderFail("删除失败");
        }

    }

}
